package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.config.BaseContext;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 加入购物车
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        // 获取用户id，设置用户id
        User user = (User) session.getAttribute("user");
         /* 菜品或者套餐是否点过
         如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
         如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
         如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
         */
        //获取购物车中的dishId或者setmealId
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        // 判断当前用户点的菜品还是套餐
        if (dishId != null){ //说明点的是菜品
            //根据dishId在dish表中查询
            lqw.eq(ShoppingCart::getDishId , dishId);
        }else {//如果不是菜品，那么一定是套餐
            //根据setmealId在setmeal表中查询
            lqw.eq(ShoppingCart::getSetmealId , setmealId);
        }

        ShoppingCart one = shoppingCartService.getOne(lqw);

        //判断one是否为nul
        if (one == null){
            //说明是新增,设置 number=1
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(user.getId());
            shoppingCartService.save(shoppingCart);
        }else {
            //说明是修改
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }

        return R.success("添加购物车成功！");
    }

    /**
     * 查询购物车
     *
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R getShoppingCart(HttpSession session) {
        //获取当前用户信息
        User user = (User) session.getAttribute("user");
        //获取当前登录用户id
        Long userId = user.getId();
        //根据用户id查询购物车
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
     * 菜品/套餐取消购物车
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        //获取当前登录用户的id
        User user = (User)session.getAttribute("user");
        Long userId = user.getId();

        //获取购物车内dishId和setmealId
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        //根据userId和(dishId/setmealId)查询到购物车内要进行修改的菜品/套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        //判断要进行修改是菜品还是套餐
        if (dishId != null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }

        ShoppingCart one = shoppingCartService.getOne(lqw);
        //判断数量是否大于1
        if (one.getNumber() > 1 ){
            one.setNumber(one.getNumber() - 1);
            shoppingCartService.updateById(one);
        }else {
            shoppingCartService.remove(lqw);
        }

        return R.success(one);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R delShoppingCart(){

        //获取当前用户id
        Long userId = BaseContext.getCurrentId();

        //根据当前登录用户id删除购物车数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(lqw);

        return R.success("清空购物车成功");
    }

}
